﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AnalisisDeRiesgos.Models.Management;
using AnalisisDeRiesgos.Models;

namespace AnalisisDeRiesgos.Controllers
{
    public class ManagementController : Controller
    {
        //
        // GET: /Management/

        public ActionResult Index()
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.Administrator))
                return RedirectToAction("LogOn", "User");

            SearchManagementModel model = new SearchManagementModel();
            
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(SearchManagementModel model)
        {
            if (ModelState.IsValid)
                model.SearchManagement(model.Pattern);
            
            return View(model);
        }

        //
        // GET: /Management/Details/5

        public ActionResult Details(int ?id)
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.Administrator))
                return RedirectToAction("LogOn", "User");

            if (id == null)
                RedirectToAction("Index");

            try
            {
                DetailsManagementModel model = new DetailsManagementModel(id.Value);

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", new ErrorModel { Message = ex.Message });
            }
        }

        //
        // GET: /Management/Create

        public ActionResult Create()
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.Administrator))
                return RedirectToAction("LogOn", "User");

            CreateManagementModel model = new CreateManagementModel();

            return View(model);
        } 

        //
        // POST: /Management/Create

        [HttpPost]
        public ActionResult Create(CreateManagementModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View();
                }

                model.Save();
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return View(model);
            }
        }
        
        //
        // GET: /Management/Edit/5
 
        public ActionResult Edit(int ?id)
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.Administrator))
                return RedirectToAction("LogOn", "User");

            if (id == null)
                return RedirectToAction("Index");
            
            try
            {
                EditManagementModel model = new EditManagementModel(id.Value);             

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", new ErrorModel { Message = ex.Message });
            }
        }

        //
        // POST: /Management/Edit/5

        [HttpPost]
        public ActionResult Edit(EditManagementModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View(model);

                model.Save();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return View(model);
        }

        //
        // GET: /Management/Delete/5
 
        public ActionResult Delete(int ?id)
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.Administrator))
                return RedirectToAction("LogOn", "User");

            if (id == null)
                return RedirectToAction("Index");
            try
            {
                DetailsManagementModel model = new DetailsManagementModel(id.Value);

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", new ErrorModel { Message = ex.Message });
            }
        }

        //
        // POST: /Management/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            DetailsManagementModel model = new DetailsManagementModel(id);
            try
            {
                model.DeleteManagement();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return View(model);
        }

        public ActionResult Error(ErrorModel errorMsg)
        {
            return View(errorMsg);
        }
    }
}
